package com.xy.mainp.main.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;

import com.xy.mainp.R;
import com.xy.mainp.databinding.ActivityMainBinding;
import com.xy.mainp.main.fragment.HomeFragment;
import com.xy.mainp.main.fragment.NovelFragment;
import com.xy.mainp.main.fragment.HuaBanFragment;
import com.xy.manager.FragmentTabManager;
import com.xy.view.TabBar;

import java.util.ArrayList;

/**
 * Created by John on 2016/10/28.
 */

public class MainCevView extends FrameLayout implements TabBar.OnClickChange {

    private ActivityMainBinding binding;
    private MainTabFragmentManager tabFragmentManager;
    private static final int MsgFragmentIndex = 0;
    private static final int HomeFragmentIndex = 1;
    private static final int MineFragmentIndex = 2;

    public MainCevView(Context context) {
        this(context, null);
    }

    public MainCevView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MainCevView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout.activity_main, this, true);
        initMain(context);
        initTabBar(context);
    }

    private void initMain(Context context) {
        tabFragmentManager = new MainTabFragmentManager(((FragmentActivity) context).getSupportFragmentManager(), R.id.fl_content_main);
    }

    private void initTabBar(Context context) {
        ArrayList<int[]> imgss = new ArrayList<>();
        imgss.add(new int[]{R.drawable.ic_msg_normal, R.drawable.ic_msg_selected});
        imgss.add(new int[]{R.drawable.ic_home_normal, R.drawable.ic_home_selected});
        imgss.add(new int[]{R.drawable.ic_mine_normal, R.drawable.ic_mine_selected});
        String[] names = new String[]{"消息", "首页", "我的"};

        binding.tabBarMain.setOnClickChange(this);
        binding.tabBarMain.setOnScreenCount(3);
        for (int i = 0; i < imgss.size(); i++) {
            TabItemView tabItemView = new TabItemView(context);
            tabItemView.setImgs(imgss.get(i));
            tabItemView.setName(names[i]);
            binding.tabBarMain.addItem(tabItemView);
            binding.tabBarMain.setClickable(true);
        }
        setPageNum(HomeFragmentIndex);
    }

    private void setPageNum(int index) {
        if (index < 0) {
            index = 1;
        }
        binding.tabBarMain.setItemClick(index);
    }

    @Override
    public boolean onClicked(int newIndex) {
        return false;
    }

    @Override
    public void onChange(int newIndex, int oldIndex) {
        tabFragmentManager.check(newIndex, oldIndex != -1);
    }

    private class MainTabFragmentManager extends FragmentTabManager {

        public MainTabFragmentManager(FragmentManager mFragmentmanger, int fragmentContentId) {
            super(mFragmentmanger, fragmentContentId);
        }


        @Override
        protected Fragment getNewFragment(int index) {
            if (index < 0) {
                return null;
            }
            Fragment fragment = new Fragment();
            switch (index) {
                case MsgFragmentIndex:
                    fragment = new HuaBanFragment();
                    break;
                case HomeFragmentIndex:
                    fragment = new HomeFragment();
                    break;
                case MineFragmentIndex:
                    fragment = new NovelFragment();
                    break;
                default:
                    fragment = new HomeFragment();
            }
            return fragment;
        }
    }
}
